package servlet.cart;

import entity.Goods;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

/**
 * 将购物车的数据取出发送给浏览器
 */
@WebServlet("/cart/show")
public class ShowCartServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession() ;
        boolean isEmpty = true ;
        //测试

        Map<Goods, Integer> cart = (Map<Goods, Integer>) session.getAttribute("cart")  ;
        if(Objects.nonNull(cart)){
            if(cart.size() > 0){
                Set<Goods> goods = cart.keySet() ;
                request.setAttribute("cartGoods" , goods);
                isEmpty = false ;
            }
        }else{
            Goods good = new Goods("1","新秀巨无霸",21.00,"汉堡","/images/1-1.png",30,1300)  ;
            Map<Goods,Integer> map = new HashMap<>() ;
            map.put(good, 2) ;
            session.setAttribute("cart" , map);
            isEmpty=false ;
        }
        request.setAttribute("isEmpty" , isEmpty);
        request.getRequestDispatcher("/WEB-INF/cart.vm").forward(request,response);
    }
}
